package com.daou.smartpush.service.company;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.daou.smartpush.domain.company.Company;
import com.daou.smartpush.domain.license.License;
import com.daou.smartpush.model.company.CompanyModel;
import com.daou.smartpush.repository.company.CompanyRepository;

/**
 * Company Service Class
 * 
 * @author seungkyupark84
 * 
 */
@Service
public class CompanyService {

    @Autowired(required = true)
    CompanyRepository companyRepository;

    public Company insetCompany(Company company) {
        Company result = companyRepository.save(company);
        return result;
    }

    @Transactional
    public List<License> getLicensesInCompany(Long companyId) {
        Company company = companyRepository.findOne(companyId);
        List<License> infos = company.getLicenses();
        return infos;
    }

    public List<CompanyModel> getAllCompanyforCreateLicesense() {
        List<Company> list = companyRepository.findAll();
        List<CompanyModel> modelList = new ArrayList<CompanyModel>();

        CompanyModel model;

        for (Company company : list) {
            model = new CompanyModel(company);
            modelList.add(model);
        }

        return modelList;
    }

    public Company selectCompany(String companyName) {
        Company company = companyRepository.findByCompanyName(companyName);

        return company;
    }
}
